package guo.xungenguser.interceptor;

import guo.xungengcommon.vo.UserVo;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 用户拦截器 只有登陆的用户才能进行操作  自定义的拦截器一定要在configuraeion配置类上添加上
 */
public class UserInterceptor implements HandlerInterceptor {

    public static final ThreadLocal<UserVo> threadLocal = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String uri = request.getRequestURI();
        boolean match = new AntPathMatcher().match("/user/gitee/save", uri);
        if (match){
            return true;
        }
        boolean match1 = new AntPathMatcher().match("/user/save", uri);
        if (match1){
            return true;
        }

        UserVo user = (UserVo) request.getSession().getAttribute("user");
        if (user == null){
            response.sendRedirect("http://auth.xungeng.com");
            return false;
        }else {
            threadLocal.set(user);
            return true;
        }
    }
}
